<?php
namespace app\admin\controller;

use app\common\model\project_model\AlbumSkinModel;
use app\common\model\project_model\AlbumSkinCategoryModel;
use app\yxwadmin\controller\Admin;
use app\admin\model\validate\AlbumSkin as ValidateSkin;

class AlbumSkin extends Admin
{

    public $error_msg = '';

    /**
     * 首页
     * @return string
     * @throws \think\Exception
     */
    public function index()
    {
        return $this->view->fetch();
    }

    /**
     * 初始化表格
     * 关键字搜索
     * 分页
     * @throws
     * @return mixed
     */
    public function getList()
    {
        $limit = input('limit','0');
        $offset = input('offset','0');
        $sortName = input('sortName','id');
        $sortOrder = input('sortOrder','desc');
        $keyword = input('keywords','');

        $model = new AlbumSkinModel();
        if ($keyword) {
            $count = $model->where('title|name|music|cate_name', 'like', '%' . $keyword . '%')->count();
            $list = $model
               ->where('title|name|music|cate_name', 'like', '%' . $keyword . '%')
               ->order([$sortName => $sortOrder])
               ->limit($offset . ',' . $limit)
               ->select();
        } else {
            $count = $model->count(); //查询满足要求的总记录数
            $list = $model
                ->order(array($sortName=>$sortOrder))
                ->limit($offset.','.$limit)
                ->select();
        }

        $data['total'] = $count;
        $data['rows'] = $list;
        return $data;
    }

    public function add() {
        if (request()->isPost()) {
            $validate = new ValidateSkin();
            $data = $_REQUEST;
            $data['created_time'] = date('Y-m-d H:i:s', time());
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $res = $validate->check($data);
            if (!$res) {
                $this->error_msg = $validate->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            $model = new AlbumSkinModel($data);
            $res = $model->allowField(true)->save();
            if (!$res) {
                $this->error_msg = $model->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }
            return json(['code' => 1, 'msg' => '新增成功', 'data' => null]);

        } else {
            $this->assign('status_list', AlbumSkinModel::statusList());
            $this->assign('is_hot_list', AlbumSkinModel::isHotList());
            $this->assign('is_remote_list', AlbumSkinModel::isRemoteList());
            $this->assign('category_list', AlbumSkinCategoryModel::categoryList());
            return $this->fetch();
        }
    }

    public function edit() {
        $id = $_REQUEST['id'];
        $model = new AlbumSkinModel();

        if (request()->isPost()) {
            $validate = new ValidateSkin();
            $data = $_REQUEST;
            $data['updated_time'] = date('Y-m-d H:i:s', time());
            $res = $validate->check($data);
            if (!$res) {
                $this->error_msg = $validate->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            $res = $model->allowField(true)->save($data,['id' => $id]);
            if ($res === false) {
                $this->error_msg = $model->getError();
                return json(['code' => 0, 'msg' => $this->error_msg, 'data' => null]);
            }

            return json(['code' => 1, 'msg' => '编辑成功', 'data' => null]);

        } else {
            $this->assign('status_list', AlbumSkinModel::statusList());
            $this->assign('is_hot_list', AlbumSkinModel::isHotList());
            $this->assign('is_remote_list', AlbumSkinModel::isRemoteList());
            $this->assign('category_list', AlbumSkinCategoryModel::categoryList());
            $Rs = $model->where(['id' => $id])->find();
            $this->assign('Rs', $Rs);
            return $this->fetch();
        }
    }

    public function view() {
        $id = input('id/d');
        $model = new AlbumSkinModel();
        $Rs = $model->where(['id' => $id])->find();
        $this->assign('Rs', $Rs);
        return $this->fetch();
    }

    /**
     * 锁定
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function lock()
    {
        $id = input('id/d');
        $model = new AlbumSkinModel();
        $where = ['id'=>$id];
        $data = $model->where($where)->find();
        $status = AlbumSkinModel::STATUS_LOCK;
        if ($data['status'] == AlbumSkinModel::STATUS_LOCK) {
            $status = AlbumSkinModel::STATUS_OK;
        }
        $res = $model->save(['status' => $status], $where);
        if ($res) {
            $ret['code']=1;
            $ret['msg']='操作成功';
        } else {
            $ret['code']=0;
            $ret['msg']='服务器繁忙';
        }
        return json($ret);
    }

}